%---------------------------------------------------------------%
%       TCP Analytical Model - Numerical Simulation             %
%---------------------------------------------------------------%

clc;

%   1) Parameters and Sceneries
%---------------------------------------------------------------%

%   Parameters

    Simulation      = 1000;
    MSS             = 1500;  
    File_SizeFactor = 10; %Mega
    BW_SizeFactor   = 10;  %Mega
    L               = 1;
    cwnd            = 2;        % TCP Specification: 2 to 4

%-------------------------------------------------%   

for i = 1:Simulation
    disp(i);
    File_Size       = 1000 * 1000  * File_SizeFactor; 
    Bandwidth       = (BW_SizeFactor * 1000 * 1000 * 8); %Bits / seconds
    count_exp       = log2(cwnd);
    EstimatedRTT    = InitialRTT();      %miliseconds
    ssthresh        = Calculate_ssThresh();
    transferredBytes= 0;
    Throughput      = cwnd * MSS;


%   2) Simulation Logic
%---------------------------------------------------------------%

    Matrix_ControlBlock        = zeros(10000, 3); %RTT, Cwnd, Throughput
    
    %Start Simulation

    
    FSMState = 'SS'; % SS (Slow Start), CA (Congestion Avoidance), FR (Fast Recovery)
    count_MB = 0;
    count_error = 0;
    
    while (transferredBytes <= File_Size)

    
        switch FSMState

              case {'SS'}         %---Slow Start State     
            
                    while (Throughput <= ssthresh)
                        hasError = rand;
                        if (hasError <= 0.75)
                            count_MB            = count_MB + 1;      
                            cwnd                = 2^(count_exp);
                            Throughput          = cwnd * MSS;
                            count_exp           = count_exp + 1;
                            transferredBytes    = transferredBytes + Throughput;
                            Matrix_ControlBlock(count_MB, 1) = Throughput;                    
                            count_error         = 0;
                        else 

                            count_error = count_error + 1;

                            if (count_error == 3)
                                FSMState    = 'FR';
                                count_error = 0;
                               ssthresh     = cwnd * MSS / 2;
                               cwnd         = (ssthresh / MSS) + 3;
                               break;
                            end
                        end
                    
                    end

                    FSMState = 'CA';

            case {'CA'} %---Congestion Avoidance State


                    while (transferredBytes <= File_Size)
                        count_MB            = count_MB + 1;      
                        cwnd                    = cwnd + 1 * (1 / cwnd); 
                        Throughput              = cwnd * MSS;
                        transferredBytes        = transferredBytes + Throughput;
                        Matrix_ControlBlock(count_MB, 1) = Throughput;                    
       
                    end

                    %FSMState = 'CA';
                    
              case {'FR'}
                        transferredBytes        = FileSize + 1;
                 
        end
     
    end

end



% Simulation Statistics

    AvgThroughput               = mean(Throughput);
    file1 = fopen('C:\Users\Rafael\Desktop\TCP Matlab Simulation\output.txt','wt');
    fprintf(file1, '%f\n', Matrix_ControlBlock(:,1));
    fclose(file1);
   

